from django.db import models
from fuguang.utils.models import BaseModel
from course.models import CourseDirectionModel, CourseCategoryModel, CourseModel
from users.models import UserModel
from orders.models import OrdersModel


class CouponModel(BaseModel):
    DISCOUNT_CHOICES = {
        (1, '减免'),
        (2, '折扣'),
    }
    COUPON_TYPE_CHOICES = {
        (0, '通用类型'),
        (1, '指定课程方向'),
        (2, '指定课程分类'),
        (3, '指定课程'),
    }
    GET_TYPE_CHOICES = {
        (0, '系统赠送'),
        (1, '用户主动领取'),
    }
    discount = models.SmallIntegerField(choices=DISCOUNT_CHOICES, default=1, verbose_name='优惠方式')
    coupon_type = models.SmallIntegerField(choices=COUPON_TYPE_CHOICES, default=0, verbose_name='优惠劵类型')
    get_type = models.SmallIntegerField(choices=GET_TYPE_CHOICES, default=0, verbose_name='优惠劵领取方式')
    total = models.IntegerField(blank=True, null=True, default=100, verbose_name='发放数量')
    has_total = models.IntegerField(blank=True, null=True, default=100, verbose_name='剩余数量')
    start_time = models.DateTimeField(verbose_name='使用时间')
    end_time = models.DateTimeField(verbose_name='过期时间')
    condition = models.IntegerField(blank=True, default=0, null=True, verbose_name='满足优惠劵使用的价格条件')
    per_limit = models.SmallIntegerField(default=1, verbose_name='每人限制领取的数量')
    sale = models.CharField(max_length=100, verbose_name='优惠公式', help_text='*表示打折，示例：*0.8表示打八折<br/>-表示满减，示例：-100表示总价上减去100')

    class Meta:
        db_table = 'fg_coupon'
        verbose_name_plural = '优惠劵'


# 优惠劵与方向
class CouponCourseDirectionModel(models.Model):
    coupon = models.ForeignKey(to=CouponModel, on_delete=models.CASCADE, related_name='to_direction', db_constraint=False, verbose_name='优惠劵')
    direction = models.ForeignKey(to=CourseDirectionModel, on_delete=models.CASCADE, related_name='to_coupon', db_constraint=False, verbose_name='专业方向')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')

    class Meta:
        db_table = 'fg_coupon_course_direction'
        verbose_name_plural = '优惠劵与专业方向'


# 优惠劵与分类
class CouponCourseCategoryModel(models.Model):
    coupon = models.ForeignKey(to=CouponModel, on_delete=models.CASCADE, related_name='to_category', db_constraint=False, verbose_name='优惠劵')
    category = models.ForeignKey(to=CourseCategoryModel, on_delete=models.CASCADE, related_name='to_coupon', db_constraint=False, verbose_name='课程分类')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')

    class Meta:
        db_table = 'fg_coupon_course_category'
        verbose_name_plural = '优惠劵与课程分类'


# 优惠劵与课程
class CouponCourseModel(models.Model):
    coupon = models.ForeignKey(to=CouponModel, on_delete=models.CASCADE, related_name='to_course', db_constraint=False, verbose_name='优惠劵')
    course = models.ForeignKey(to=CourseModel, on_delete=models.CASCADE, related_name='to_coupon', db_constraint=False, verbose_name='课程')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')

    class Meta:
        db_table = 'fg_coupon_course'
        verbose_name_plural = '优惠劵与课程信息'


# 优惠劵管理类
class CouponLogModel(BaseModel):
    USE_STATUS_CHOICES = {
        (0, '未使用'),
        (1, '已使用'),
        (2, '已过期'),
    }
    # 赠送给用户，是否使用  使用的时间，针对哪个订单使用
    # 与订单关联，等订单完成，再回来修改
    user = models.ForeignKey(to=UserModel, on_delete=models.CASCADE, related_name='to_coupon', db_constraint=False, verbose_name='用户')
    coupon = models.ForeignKey(to=CouponModel, on_delete=models.CASCADE, related_name='to_user', db_constraint=False, verbose_name='优惠劵')
    use_time = models.DateTimeField(null=True, blank=True, verbose_name='使用时间')
    use_status = models.SmallIntegerField(choices=USE_STATUS_CHOICES, default=0, verbose_name='使用状态')
    order = models.ForeignKey(to=OrdersModel, on_delete=models.DO_NOTHING, null=True, blank=True, related_name='to_coupon', db_constraint=False, verbose_name='订单')

    class Meta:
        db_table = 'fg_coupon_log'
        verbose_name_plural = '优惠劵发放与日志管理类'

    def __str__(self):
        return f"{self.coupon.name}发放和使用日志"
